package com.cos.shop.services.impl;

import com.cos.shop.dao.CartDao;
import com.cos.shop.dao.ProductDao;
import com.cos.shop.dao.impl.CartDaoImpl;
import com.cos.shop.dao.impl.ProductDaoImpl;
import com.cos.shop.entity.Cart;
import com.cos.shop.entity.Product;
import com.cos.shop.services.CartServices;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author: COS
 * @time: 2022/2/24 11:05
 * @description:
 */
public class CartServicesImpl implements CartServices {
    private CartDao cd = new CartDaoImpl();

    @Override
    public void updateCart(int uid, int pid, int num, double prices) {
        try {
            if (num!=0) {
                //使用uid+pid查询对应的购物车数据
                Cart cart = cd.getCartByUidAndPid(uid, pid);
                //1 num=1 数值+1  num=-1 数值-1
                // 2  num =1   =>2+1=3
                // 2  num =-1  =>2+-1=1
                //设置最新的商品数量
                cart.setCnum(cart.getCnum()+num);
                //重新设置小计价格
                cart.setCcount(cart.getCnum()*prices);
                //修改
                cd.updateCart(cart);
            }else{
                //2 num=0 删除单个购物车数据
                cd.deleteCart(uid,pid);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean clearCart(int pid, int uid) {
        try {
            //判断 rg pid是0就要清空购物车
            int i=0;
            if (pid==0) {
                //清空购物车
                i=cd.clearCart(uid);
            }else{
                //删除单个购物车数据
                i=cd.deleteCart(uid,pid);
            }
            if (i>0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<Cart> getCart(int uid) {
        try {
            if (uid>0) {
                return cd.getCartByUid(uid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean createCart(int uid, int pid) {
        try {
            //1 先使用uid , pid 购物车   链表查
            //为了后面能够使用到商品的数据,这里需要进行商品和购物车额连表查询
            Cart c = cd.getCartByUidAndPid(uid, pid);
            int i = 0;
            if (c != null) {
                //购物已有该商品 修改
                //商品数量+1 价格小计    2   40 3=>60
                c.setCnum(c.getCnum() + 1);
                //小计 数量*单价
                c.setCcount(c.getCnum() * c.getPro().getPprice());
                //update 更新数据
                i = cd.updateCart(c);
            } else {
                //购物没有商品 新增
                Cart cart = new Cart();
                cart.setCnum(1);
                //查询商品数据
                ProductDao pd = new ProductDaoImpl();
                Product pro = pd.getProductById(pid);
                cart.setPid(pid);
                cart.setCcount(pro.getPprice());
                cart.setUid(uid);
                //新增
                i = cd.addCart(cart);
            }
            if (i > 0) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return false;
    }
}
